/*
 * Copyright (c) 2006-2019, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-05-24                  the first version
 */

#include <rthw.h>
#include <rtthread.h>
#include "gd32f4xx.h"
#include "gd32f450i_eval.h"


extern void system_clock_config(void);
extern void SystemCoreClockUpdate(void);
extern uint32_t SystemCoreClock;


#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
/*
 * Please modify RT_HEAP_SIZE if you enable RT_USING_HEAP
 * the RT_HEAP_SIZE max value = (sram size - ZI size), 1024 means 1024 bytes
 */
#define RT_HEAP_SIZE (30*1024)
static rt_uint8_t rt_heap[RT_HEAP_SIZE];

RT_WEAK void *rt_heap_begin_get(void)
{
    return rt_heap;
}

RT_WEAK void *rt_heap_end_get(void)
{
    return rt_heap + RT_HEAP_SIZE;
}
#endif

void rt_os_tick_callback(void)
{
    rt_interrupt_enter();
    
    rt_tick_increase();

    rt_interrupt_leave();
}

void SysTick_Handler(void)
{
	 rt_os_tick_callback();
}

static uint32_t _SysTick_Config(rt_uint32_t ticks)
{
   if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
  {
    return (1UL);                                                   /* Reload value impossible */
  }

  SysTick->LOAD  = (uint32_t)(ticks - 1UL);                         /* set reload register */
  NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
  SysTick->VAL   = 0UL;                                             /* Load the SysTick Counter Value */
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                         /* Enable SysTick IRQ and SysTick Timer */
  return (0UL);                                                     /* Function successful */

}


/**
 * This function will initial your board.
 */
void rt_hw_board_init(void)
{

    /* 
     * TODO 1: OS Tick Configuration
     * Enable the hardware timer and call the rt_os_tick_callback function
     * periodically with the frequency RT_TICK_PER_SECOND. 
     */
     _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
    /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

#ifdef RT_USING_CONSOLE

static int uart_init(void)
{
    gd_eval_com_init(EVAL_COM0);
    return 0;
}
INIT_BOARD_EXPORT(uart_init);

void rt_hw_console_output(const char *str)
{
   rt_size_t i = 0, size = 0;
    char a = '\r';

   

    size = rt_strlen(str);
    for (i = 0; i < size; i++)
    {
        if (*(str + i) == '\n')
        {
           
					usart_data_transmit(EVAL_COM0, (uint8_t) a);
					while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
        }
        
				usart_data_transmit(EVAL_COM0, (uint8_t) (*(str + i)));
				while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
    }
}
char rt_hw_console_getchar(void)
{
    int ch = -1;

    if (usart_flag_get(EVAL_COM0, USART_FLAG_RBNE) != RESET)
    {
			
        ch = usart_data_receive(EVAL_COM0);
    }
    else
   {
//        if(usart_flag_get(USART0,  USART_FLAG_ORERR) != RESET)
//        {
//					usart_flag_clear(USART0,USART_FLAG_ORERR);
//           // __HAL_UART_CLEAR_OREFLAG(&UartHandle);
//        }
       rt_thread_mdelay(10);
    }
    return ch;
}

#endif

